﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Category.ascx.cs" Inherits="BSPortal.Usercontrol.Admin.Book.Category" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

<telerik:RadWindowManager ID="RadWindowManager2" runat="server" 
    Skin="Outlook">
 </telerik:RadWindowManager>
 <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">     
 </telerik:RadAjaxManager>
    <style type="text/css">      
        .bigModuleBottom td{padding:0}
    </style>
    <script type="text/javascript">        

        function GetRadWindow() {
            var oWindow = null;
            if (window.radWindow)
                oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog      
            else if (window.frameElement.radWindow)
                oWindow = window.frameElement.radWindow; //IE (and Moz as well)      
            return oWindow;
        }

        function Close() {
            var arg = new Object();

            GetRadWindow().Close(arg);
        }
        function onClientContextMenuShowing(sender, args) {
            var treeNode = args.get_node();
            treeNode.set_selected(true);
            //enable/disable menu items
            setMenuItemsState(args.get_menu().get_items(), treeNode);
        }

        function onClientContextMenuItemClicking(sender, args) {
            var menuItem = args.get_menuItem();
            var treeNode = args.get_node();
            menuItem.get_menu().hide();

            switch (menuItem.get_value()) {
                case "Copy":
                    break;
                case "Rename":
                    treeNode.startEdit();
                    break;
                case "NewFolder":
                    break;
                case "MarkAsRead":
                    break;
                case "Delete":
                    var result = confirm("Bạn có chắc muốn xóa: " + treeNode.get_text());
                    args.set_cancel(!result);
                    break;
            }
        }

        //this method disables the appropriate context menu items
        function setMenuItemsState(menuItems, treeNode) {
            for (var i = 0; i < menuItems.get_count(); i++) {
                var menuItem = menuItems.getItem(i);
                switch (menuItem.get_value()) {
                    case "Copy":
                        formatMenuItem(menuItem, treeNode, 'Copy "{0}"');
                        break;
                    case "Rename":
                        formatMenuItem(menuItem, treeNode, 'Rename "{0}"');
                        break;
                    case "Delete":
                        formatMenuItem(menuItem, treeNode, 'Delete "{0}"');
                        break;
                    case "NewFolder":
//                        if (treeNode.get_parent() == treeNode.get_treeView()) {
//                            menuItem.set_enabled(false);
//                        }
//                        else {
//                            menuItem.set_enabled(true);
//                        }
                        break;
                    case "MarkAsRead":
                        var enabled = hasNodeMails(treeNode);
                        menuItem.set_enabled(enabled != null);
                        break;
                }
            }
        }

        //formats the Text of the menu item
        function formatMenuItem(menuItem, treeNode, formatString) {
            var nodeValue = treeNode.get_value();
            if (nodeValue && nodeValue.indexOf("_Private_") == 0) {
                menuItem.set_enabled(false);
            }
            else {
                menuItem.set_enabled(true);
            }
            var newText = String.format(formatString, extractTitleWithoutMails(treeNode));
            menuItem.set_text(newText);
        }

        //checks if the text contains (digit)
        function hasNodeMails(treeNode) {
            return treeNode.get_text().match(/\([\d]+\)/ig);
        }

        //removes the brackets with the numbers,e.g. Inbox (30)
        function extractTitleWithoutMails(treeNode) {
            return treeNode.get_text().replace(/\s*\([\d]+\)\s*/ig, "");
        }      
        </script>
     <div>
 <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
                </telerik:RadAjaxLoadingPanel>
                <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" LoadingPanelID="RadAjaxLoadingPanel1">
                    <telerik:RadTreeView ID="RadTreeView1" runat="server" AllowNodeEditing="true" OnContextMenuItemClick="RadTreeView1_ContextMenuItemClick"
                        OnClientContextMenuItemClicking="onClientContextMenuItemClicking" OnClientContextMenuShowing="onClientContextMenuShowing"
                        OnNodeEdit="RadTreeView1_NodeEdit">
                        <ContextMenus>
                            <telerik:RadTreeViewContextMenu ID="MainContextMenu" runat="server">
                                <Items>
                                    <telerik:RadMenuItem Value="Rename" Text="Sửa ..." Enabled="false" PostBack="false">
                                    </telerik:RadMenuItem>
                                    <telerik:RadMenuItem IsSeparator="true">
                                    </telerik:RadMenuItem>
                                    <telerik:RadMenuItem Value="NewFolder" Text="Thêm mới">
                                    </telerik:RadMenuItem>
                                    <telerik:RadMenuItem IsSeparator="true">
                                    </telerik:RadMenuItem>
                                    <telerik:RadMenuItem Value="Delete" Text="Xóa ...">
                                    </telerik:RadMenuItem>
                                </Items>
                                <CollapseAnimation Type="none" />
                            </telerik:RadTreeViewContextMenu>
                            <telerik:RadTreeViewContextMenu ID="EmptyFolderContextMenu" runat="server">
                                <Items>
                                    <telerik:RadMenuItem Value="NewFolder" Text="Thêm mới">
                                    </telerik:RadMenuItem>
                                    <telerik:RadMenuItem IsSeparator="true">
                                    </telerik:RadMenuItem>
                                    <telerik:RadMenuItem Value="EmptyFolder" Text="Empty this folder">
                                    </telerik:RadMenuItem>
                                </Items>
                                <CollapseAnimation Type="none" />
                            </telerik:RadTreeViewContextMenu>
                        </ContextMenus>
                        <Nodes>
                            
                        </Nodes>
                    </telerik:RadTreeView>
                </telerik:RadAjaxPanel>
                </div>